package com.woniuxy;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentTypeService {

    @Autowired
    StudenttypeMapper studenttypeMapper;

    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    private List<Studenttype> studenttypes=null;

//    public List<Studenttype> getAll(){
//        if(studenttypes == null) {
//            studenttypes = studenttypeMapper.selectList(null);
//            System.out.println("缓存为空, 从数据库中查询数据");
//        }else{
//            System.out.println("缓存中有值,直接返回缓存数据");
//        }
//        return studenttypes;
//    }

    public Studenttype getById(Integer typeId){
        Studenttype studenttype = studenttypeMapper.selectById(typeId);
        return studenttype;
    }

    public void add(Studenttype studenttype){
        int insert = studenttypeMapper.insert(studenttype);

        studenttypes = null;
    }

    public void deleteById(Integer typeId){
        int i = studenttypeMapper.deleteById(typeId);
        studenttypes = null;
    }

    public void updateById(Studenttype studenttype){
        int i = studenttypeMapper.updateById(studenttype);
        studenttypes = null;
    }

    public Page<Studenttype> pageByType(Integer currentPage, Integer pageSize) {
        ValueOperations<String, Object> ops = redisTemplate.opsForValue();
        Integer currentPage1 = (Integer) ops.get("currentPage");
        if (currentPage1!=null && currentPage1!=currentPage){
        Page<Studenttype> page = new Page<>(currentPage,pageSize);
        Page<Studenttype> studentPageResult = studenttypeMapper.selectPage(page, null);
        return studentPageResult;
    }
        return null;
    }

//    public Page<Studenttype> pageByType(Integer currentPage, Integer pageSize, Integer id) {
//
//        //分页查询条件
//        Page<Studenttype> page = new Page<>(currentPage,pageSize);
//        QueryWrapper<Studenttype> bookQueryWrapper = new QueryWrapper<>();
//        bookQueryWrapper.eq("id",id);
//        Page<Studenttype> studentPageResult = studenttypeMapper.selectPage(page, bookQueryWrapper);
//
//        return studentPageResult;
//    }


}
